﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：BidProductAdd.cs
    // 文件功能描述：采购产品管理：新增单个采购产品
    // 创建标识：吕海乐2009-03-24
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class BidProductAdd : BasePage
    {
        public BidProductAdd()
        {
            //设置页面 模块以及操作类型
            this.ModuleNo = Module.采购产品管理.ToString();
            this.PageOperateType = OperateType.新增采购产品.ToString();
        }
        
        /// <summary>
        /// 返回页面
        /// </summary>
        public string strBackUrl { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            Uri Url = HttpContext.Current.Request.UrlReferrer;
            strBackUrl = Url.AbsolutePath.Substring(Url.AbsolutePath.LastIndexOf('/')+1 , Url.AbsolutePath.Length-1- Url.AbsolutePath.LastIndexOf('/'));
            if (!IsPostBack)
            {
                //绑定选择的产品信息
                if (Request["goodsid"] != null)
                {
                    string goodsId = Request.QueryString["goodsid"];
                    string SortSource = Request.QueryString["SortSource"];
                    int totalCount = 0;

                    IBLL_Goods ig = Factory.BLL_Goods();
                    Model.View.Goods goodsModel = ig.GetGoodsByID(goodsId);
                    if (goodsModel != null)
                    {
                        txtGoodsName.Value = goodsModel.GoodsName;
                        txtCompany_SC.Value = goodsModel.CompanyName_SC;
                        txtCompany_TB.Value = goodsModel.CompanyName_TB;
                        //ddlSortSource.SelectedValue = goodsModel.Source.Value.ToString();
                        if (SortSource.Equals("3"))
                        {
                            txtProcurePrice.Text = goodsModel.LimitPrice.ToString().Substring(0, goodsModel.BidPrice.ToString().Length - 1);
                        }
                        else
	                    {
                            txtProcurePrice.Text = goodsModel.BidPrice.ToString().Substring(0,goodsModel.BidPrice.ToString().Length-1);
	                    }
                                            
                        hfdSortSource.Value = goodsModel.Source.Value.ToString();
                        hfdGoodsId.Value = goodsId;
                    }
                    else
                    { 
                        PageError("产品不存在！", "");
                    }
                }

                //绑定所有项目
                IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
                ProcurementProject procurementProject = new ProcurementProject();
                procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                int temp;
                IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                ddlProject.DataSource = projectList;
                ddlProject.DataTextField = "ProjectName";
                ddlProject.DataValueField = "ProjectID";
                ddlProject.DataBind();
                //绑定从前一页面传过来的project
                if (!string.IsNullOrEmpty(Request.QueryString["projectID"]))
                {
                    for (int i = 0; i < ddlProject.Items.Count; i++)
                    {
                        if (ddlProject.Items[i].Value == Request.QueryString["projectID"])
                        {
                            ddlProject.Items[i].Selected = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 根据产品的来源属性计算出其采购限价
        /// 对于非中标药采购限价计算规则
        ///  >575 -75
        ///  <=575  /1.15
        ///
        /// </summary>
        /// <param name="goodsModel"></param>
        /// <returns></returns>
        private string GetProcurePrice(HZ.DrugTrade.Model.View.Goods goodsModel)
        {
            if (goodsModel.Source <= 1)
            {
                return goodsModel.BidPrice.Value.ToString("F");
            }
            else
            {
                if (goodsModel.LimitPrice != null)
                {
                    if (goodsModel.LimitPrice.Value <= 575)
                    {
                        return (goodsModel.LimitPrice.Value / (decimal)1.15).ToString("F");
                    }
                    else
                    {
                        return (goodsModel.LimitPrice.Value - 75).ToString("F");
                    }
                }
                else
                {
                    return "0.00";
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPatchJoin_Click(object sender, EventArgs e)
        {
            HZ.Web.WebUtil.RedirectByScript("BidProductPatchAdd.aspx");
        }

        /// <summary>
        /// 添加采购产品
        /// </summary>
        private void AddBidProduct()
        {
            //判断采购价格是否为空
            if (string.IsNullOrEmpty(txtProcurePrice.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("采购价格不能为空！", this);
                txtProcurePrice.Focus();
                return;
            }
            //获取新增采购产品的相关信息
            ProcureCatalog procureCatalog = new ProcureCatalog();

            procureCatalog.ProcureCatalogID = Guid.NewGuid().ToString();
            if (!string.IsNullOrEmpty(hfdGoodsId.Value))
            {
                procureCatalog.GoodsID = hfdGoodsId.Value.ToString();
                procureCatalog.CompanyIDs_PS = companyId.Value.TrimEnd(',');
                procureCatalog.CompanyNames_PS = hfdCompanyName.Value.TrimEnd(',');

                if (txtProcurePrice.Text.Trim() != "")
                {
                    procureCatalog.ProcurePrice = Convert.ToDecimal(txtProcurePrice.Text.Trim());
                }
                procureCatalog.ProjectID = ddlProject.SelectedItem.Value;
                //procureCatalog.SortSource = Convert.ToInt32(ddlSortSource.SelectedValue);

                procureCatalog.IsUsing = true;
                procureCatalog.IsFlowing = true;
                procureCatalog.AddTime = DateTime.Now;

                //使用方法，执行操作，添加新的采购产品
                IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
                int flag = ipb.AddProcureCatalog(procureCatalog);
                if (flag == 1)
                {
                    PageInfo("添加成功！", "BidProductList.aspx");
                }
                else if (flag == 2)
                {
                    PageError("该商品已经存在于这个项目中，无法重复添加！", "BidProductList.aspx");
                }
                else if (flag == 0)
                {
                    PageError("添加失败！", this.ReturnUrl);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("未选择任何产品！", this);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                AddBidProduct();
            }
        }

    }
}
